const system = wx.getSystemInfoSync();
const app = getApp(),
  pageSize = 15,
  typeOptions = [{
    value: "",
    name: "全部"
  }, {
    value: 1,
    name: "收入"
  }, {
    value: 2,
    name: "支出"
  }];
Page({

  data: {
    x: system.windowWidth - 50,
    y: system.windowHeight - 150,
    loadmore: false, //加载更多
    nomore: false, //没有更多
    list: [], //列表数据
    showSearch: false,
    empty: false,
    typeOptions: typeOptions, //类型
    sourceOptions: [], //来源选项列表
    conditions: {
      startTime: '',
      endTime: "",
      orderNum: "", //订单编号
      typeIndex: "", //类型索引
      sourceIndex: "", //来源选择的索引
    },
    drawer: false,
    isFilter: false,
  },



  //钩子函数
  onLoad: function (options) {
    this.id = options.id;
    this.page = 1;
    this.getSourceConfig();
    this.getFundRecord();
  },


  reloadData() {
    this.page = 1;
    this.setData({
      list: [], //列表数据
      loadmore: false,
      nomore: false,
    })
    this.getFundRecord();
  },


  navRecord() {
    wx.navigateTo({
      url: '../consumeRecord/consumeRecord?id=' + this.id,
    })
  },


  //获取资金记录
  getFundRecord() {
    app.ajax({
      url: "Partner/WaterCard/waterCardCapitalRecordList",
      data: {
        id: this.id,
        page: this.page,
        row: pageSize,
        btype: this.data.conditions.sourceIndex !== "" ? this.data.sourceOptions[this.data.conditions.sourceIndex].value : "",
        type: this.data.conditions.typeIndex !== "" ? this.data.typeOptions[this.data.conditions.typeIndex].value : '',
        start_time: this.data.conditions.startTime,
        end_time: this.data.conditions.endTime,
        order_number: this.data.conditions.orderNum
      },
      success: res => {
        wx.hideLoading()
        if (res.statusCode == 200) {
          if (res.data.code == 1000) {
            const {
              page_count: totalPage
            } = res.data.data.page;
            const source = res.data.data.data;
            let list;
            if (this.page == 1) {
              list = source;
            } else {
              list = this.data.list.concat(source);
            }
            this.setData({
              loadmore: this.page < totalPage,
              nomore: this.page >= totalPage,
              list,
              drawer: false,
              empty: totalPage == 0 ? true : false,
            })
          } else {
            app.showToast(res.data.message)
          }
        } else {
          app.showToast("网路错误!请稍后再试~~")
        }
      },
      error: e => {
        wx.hideLoading()
      }
    })
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.nomore) {
      return false;
    }
    ++this.page;
    this.getFundRecord();
  },



  //获取来源配置
  getSourceConfig() {
    app.ajax({
      url: "Common/Common/getCardCapitalRecordConfig",
      success: res => {
        if (res.data.code == 1000) {
          const config = res.data.data;
          let options = [{
            value: "",
            name: "全部"
          }];
          for (let x in config) {
            options.push({
              value: x,
              name: config[x]
            })
          }
          this.setData({
            sourceOptions: options
          })
        }
      }
    })
  },


  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },



  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  handleCondition(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },
  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },

})